//
//  YouJiViewController.m
//  MyProject
//
//  Created by apple on 15/11/5.
//  Copyright (c) 2015年 apple. All rights reserved.
//

#import "YouJiViewController.h"
#import "attraction_tripsModel.h"
#import "JHRefresh.h"
#import "AFNetworking.h"
#import "ScrollViewTableViewCell.h"
#import "LZXHelper.h"
#import "MMProgressHUD.h"

#define URL @"http://chanyouji.com/api/attractions/%@.json?attraction_trips=true&"

@interface YouJiViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSMutableArray *_datArr;
}

@property (nonatomic,assign)NSInteger page;
@property (nonatomic,strong)UITableView *tableView;
@end

@implementation YouJiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.page = 1;
    
    _datArr = [[NSMutableArray alloc]init];
    
    [self getData];
    
    
    [self createView];
    
}

-(void)getData
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:[NSString stringWithFormat:URL,self.ID] parameters:@{@"page":@(self.page)} success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSArray * arr = dic[@"attraction_trips"];
        
        if (self.page == 1) {
            [_datArr removeAllObjects];
        }
        for (NSDictionary * subDic in arr) {
            attraction_tripsModel * model = [[attraction_tripsModel alloc]initWithDictionary:subDic error:nil];
            if (model) {
                [_datArr addObject:model];
            }
            
        }
        
        [_tableView reloadData];
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
}

-(void)createView
{
    _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:0];
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    //注册
    [_tableView registerNib:[UINib nibWithNibName:@"ScrollViewTableViewCell" bundle:nil] forCellReuseIdentifier:@"567"];
    
    //刷新
    __weak typeof(self)weakSelf = self;
    [_tableView addRefreshHeaderViewWithAniViewClass:[JHRefreshCommonAniView class] beginRefresh:^{
        weakSelf.page = 1;
        [weakSelf getData];
        [weakSelf.tableView headerEndRefreshingWithResult:JHRefreshResultSuccess];
    }];
    //加载
    [_tableView addRefreshFooterViewWithAniViewClass:[JHRefreshAmazingAniView class] beginRefresh:^{
        weakSelf.page ++;
        [weakSelf getData];
        [weakSelf.tableView footerEndRefreshing];
    }];
    
    
}


#pragma mark - 代理方法 － 
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _datArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    attraction_tripsModel * model = [_datArr objectAtIndex:indexPath.row];
    ScrollViewTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"567"];
    [cell createScrollViewWithArr:model.notes];
    cell.descLable.text = model.comment;
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    attraction_tripsModel * model = [_datArr objectAtIndex:indexPath.row];
    float height = [LZXHelper textHeightFromTextString:model.comment width:[UIScreen mainScreen].bounds.size.width fontSize:15];
    return height + 110;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
